package com.starblink.store.manage.adapter

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import com.guadou.lib_baselib.view.span.dsl.buildSpannableString
import com.starblink.android.basic.aroute.CommonRoute
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.util.FontUtil
import com.starblink.basic.util.CommUtils
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.store.R
import com.starblink.store.databinding.ItemManageStoreBinding

class ItemStoreManageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleRes) {
    lateinit var merchat: MerchantWebVOF

    val binding: ItemManageStoreBinding = ItemManageStoreBinding.inflate(inflater, this, true)

    companion object {
    }


    fun bindPost(
        merchat: MerchantWebVOF,
        selects: MutableList<MerchantWebVOF> = mutableListOf(),
        hasSelect: Boolean = true
    ) {
        this.merchat = merchat
        binding.ivSelect.visibleOrGone(hasSelect) {
            this.setImageResource(if (selects.contains(merchat)) R.mipmap.icon_store_selected else R.mipmap.icon_store_select)
        }
        binding.manage.tvShop.click {
            CommonRoute.startStoreDetail(context, merchat.id)
        }

        binding.manage.title.text = merchat.merchantName
        binding.manage.ivStore.load(merchat.logo ?: "", merchat.merchantName)
        binding.manage.tags.tvFollowers.text =
            "${merchat.subNum} ${CommUtils.getString(com.starblink.basic.style.R.string.following_2)}"
        binding.manage.tags.sale.visibleOrGone(merchat.isOnSale == true) {}
        binding.manage.tags.divShop.visibleOrGone(merchat.merchantDiscountInfo != null) {}
        binding.manage.tags.ivShop.visibleOrGone(merchat.merchantDiscountInfo != null) {
            var info: MerchantWebVOF.MerchantDiscountInfo? = merchat.merchantDiscountInfo
            buildSpannableString {
                if (!info?.prefix.isNullOrEmpty()) {
                    addText("${info?.prefix} ") {
                        setColor(Color.parseColor("#F55858"))
                        FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                            setTypeface(it)
                        }
                    }
                }
                addText(info?.discount ?: "") {
                    setColor(Color.parseColor("#F55858"))
                    setScale(1.2f)
                    FontUtil.getTypeface(FontUtil.MONTSERRAT_BOLD)?.let {
                        setTypeface(it)
                    }
                }
                if (!info?.suffix.isNullOrEmpty()) {
                    addText(" ${info?.suffix}") {
                        setColor(Color.parseColor("#F55858"))
                        FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM)?.let {
                            setTypeface(it)
                        }
                    }
                }
            }
        }
    }
}